import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import request from "@/utils/request";
let newRoutes = []
const routes = [{
        path: "/login",
        name: "Login",
        component: (resolve) => require(["@/views/login/login"], resolve),
    },
    {
        path: "/",
        name: "home",
        component: (resolve) => require(["@/views/Home"], resolve),
        redirect: "/welcome",
        children: [{
            path: "/welcome", //首页
            name: "welcome",
            component: (resolve) => require(["@/views/main/welcome"], resolve),
        }, ]
    }

    // {
    //     path: "/merchant/material_list", //物料列表
    //     name: "material_list",
    //     component: (resolve) => require(["@/views/main/merchant/material_list"], resolve),
    // },
    // {
    //     path: "/money/dimension_string", //分账串
    //     name: "dimension_string",
    //     component: (resolve) => require(["@/views/main/money/dimension_string"], resolve),
    // },
    // {
    //     path: "/money/dimension_record", //分账记录
    //     name: "dimension_record",
    //     component: (resolve) => require(["@/views/main/money/dimension_record"], resolve),
    // },
    // {
    //     path: "/money/trad_record", //交易记录
    //     name: "trad_record",
    //     component: (resolve) => require(["@/views/main/money/trad_record"], resolve),
    // },
    // {
    //     path: "/money/scord_record", //积分记录
    //     name: "scord_record",
    //     component: (resolve) => require(["@/views/main/money/scord_record"], resolve),
    // },
    // {
    //     path: "/money/payment_record", //汇付关联支付
    //     name: "payment_record",
    //     component: (resolve) => require(["@/views/main/money/payment_record"], resolve),
    // },
    // {
    //     path: "/money/return_score", //待返积分
    //     name: "return_score",
    //     component: (resolve) => require(["@/views/main/money/return_score"], resolve),
    // },


]
const routesArr = [{
        path: "/welcome", //首页
        name: "welcome",
        component: (resolve) => require(["@/views/main/welcome"], resolve),
    },
    //系统
    {
        path: "/system/manage/system_role", //角色管理
        name: "system_role",
        component: (resolve) => require(["@/views/main/system/manage/system_role"], resolve),
    },
    {
        path: "/system/manage/managers_list", //管理员列表
        name: "sys_admin",
        component: (resolve) => require(["@/views/main/system/manage/managers_list"], resolve),
    },
    {
        path: "/system/manage/rules", //权限规则
        name: "rules",
        component: (resolve) => require(["@/views/main/system/manage/rules"], resolve),
    },
    //配置项
    {
        path: "/configure/notice/index", //公告
        name: "notice",
        component: (resolve) => require(["@/views/main/configure/notice/index"], resolve),
    },
    //商城管理 酒店
    {
        path: "/shopSystem/hotel/hotel_list", //酒店列表
        name: "hotel_list",
        component: (resolve) => require(["@/views/main/shopSystem/hotel/hotel_list"], resolve),
    },
    {
        path: "/shopSystem/hotel/order_list", //酒店订单
        name: "order_list",
        component: (resolve) => require(["@/views/main/shopSystem/hotel/order/order_list"], resolve),
    },
    //商城管理 商品
    {
        path: "/shopSystem/allShop/shop_list", //商品列表
        name: "shop_list",
        component: (resolve) => require(["@/views/main/shopSystem/allShop/shop_list"], resolve),
    },
    {
        path: "/shopSystem/allShop/shop_classification", //商品分类
        name: "shop_classification",
        component: (resolve) => require(["@/views/main/shopSystem/allShop/shop_classification"], resolve),
    },
    {
        path: "/shopSystem/allShop/shop_comment", //商品评论
        name: "shop_comment",
        component: (resolve) => require(["@/views/main/shopSystem/allShop/shop_comment"], resolve),
    },
    //商城管理 积分
    {
        path: "/shopSystem/scoreShop/shop_list", //积分商品列表
        name: "score_shop_list",
        component: (resolve) => require(["@/views/main/shopSystem/scoreShop/shop_list"], resolve),
    },
    //商城管理 订单管理 
    {
        path: "/shopSystem/order/online_order", //电商订单
        name: "online_order",
        component: (resolve) => require(["@/views/main/shopSystem/order/online_order"], resolve),
    },
    {
        path: "/shopSystem/order/score_order", //积分订单
        name: "score_order",
        component: (resolve) => require(["@/views/main/shopSystem/order/score_order"], resolve),
    },
    {
        path: "/shopSystem/order/offline_order", //线下订单
        name: "offline_order",
        component: (resolve) => require(["@/views/main/shopSystem/order/offline_order"], resolve),
    },
    //营销 首页推荐
    {
        path: "/sale/indexRecommend/index_list", //首页推荐  推荐列表
        name: "indexindex_list",
        component: (resolve) => require(["@/views/main/sale/indexRecommend/index_list"], resolve),
    },
    {
        path: "/sale/indexRecommend/configure_list", //首页推荐  标记配置推荐
        name: "configure_list",
        component: (resolve) => require(["@/views/main/sale/indexRecommend/configure_list"], resolve),
    },
    //营销 首页配置
    {
        path: "/sale/indexConfig/nav_config", //导航
        name: "nav_config",
        component: (resolve) => require(["@/views/main/sale/indexConfig/nav_config"], resolve),
    },
    {
        path: "/sale/indexConfig/find_goods", //发现好货
        name: "find_goods",
        component: (resolve) => require(["@/views/main/sale/indexConfig/find_goods"], resolve),
    },
    {
        path: "/sale/indexConfig/dapai", //大牌荟选
        name: "dapai",
        component: (resolve) => require(["@/views/main/sale/indexConfig/dapai"], resolve),
    },
    {
        path: "/sale/indexConfig/banner_list", //banner
        name: "banner",
        component: (resolve) => require(["@/views/main/sale/indexConfig/banner_list"], resolve),
    },
    {
        path: "/sale/indexConfig/activity", //活动
        name: "activity",
        component: (resolve) => require(["@/views/main/sale/indexConfig/activity"], resolve),
    },
    {
        path: "/sale/indexConfig/select", //精选新品
        name: "goods",
        component: (resolve) => require(["@/views/main/sale/indexConfig/select"], resolve),
    },
    //营销 电商推荐
    {
        path: "/sale/onlineRecommend/index_list", //首页推荐  推荐列表
        name: "indexonline_list",
        component: (resolve) => require(["@/views/main/sale/onlineRecommend/index_list"], resolve),
    },
    {
        path: "/sale/onlineRecommend/banner", //banner
        name: "online_banner",
        component: (resolve) => require(["@/views/main/sale/onlineRecommend/banner"], resolve),
    },
    {
        path: "/sale/onlineRecommend/config_list", //首页推荐  标记配置推荐
        name: "config_list",
        component: (resolve) => require(["@/views/main/sale/onlineRecommend/config_list"], resolve),
    },
    //营销 红包
    {
        path: "/sale/hongbao/index", //红包
        name: "hongbao_list",
        component: (resolve) => require(["@/views/main/sale/hongbao/index"], resolve),
    },
    //会员管理
    {
        path: "/member/user/user_list", //会员列表
        name: "user_list",
        component: (resolve) => require(["@/views/main/member/user_list"], resolve),
    },
    //商户 店铺管理
    {
        path: "/merchant/shopList/shop_list",
        name: "store_list",
        component: (resolve) => require(["@/views/main/merchant/shopList/shop_list"], resolve),
    },
    //商户 商家进件
    {
        path: "/merchant/business/setting_in", //入驻申请列表
        name: "setting_in",
        component: (resolve) => require(["@/views/main/merchant/setting_in"], resolve),
    },
    //商户 区域代理
    {
        path: "/merchant/region_agent/index", //入驻申请列表
        name: "agent_index",
        component: (resolve) => require(["@/views/main/merchant/region_agent/index"], resolve),
    },
    //财务
    {
        path: "/money/withdrawal/wait_for_judg", //待审核
        name: "wait_for_judg",
        component: (resolve) => require(["@/views/main/money/withdrawal/wait_for_judg"], resolve),
    },
    {
        path: "/money/withdrawal/judg_list", //提现归档
        name: "judg_list",
        component: (resolve) => require(["@/views/main/money/withdrawal/judg_list"], resolve),
    },
    {
        path: "/money/withdrawal/examine_list", //审核列表
        name: "examine_list",
        component: (resolve) => require(["@/views/main/money/withdrawal/examine_list"], resolve),
    },
    //工作台
    {
        path: "/work/order/order_query", //订单查询
        name: "order_query",
        component: (resolve) => require(["@/views/main/work/order/order_query"], resolve),
    },
    {
        path: "/work/order/business_info", //商家信息
        name: "business_info",
        component: (resolve) => require(["@/views/main/work/order/business_info"], resolve),
    },
    {
        path: "/work/score/score_list", //积分列表
        name: "score_list",
        component: (resolve) => require(["@/views/main/work/score/score_list"], resolve),
    },
]

const router = new VueRouter({
    mode: 'history',
    routes,
    base: "/",
});

//菜单接口
function getMenu(data) {
    return request({
        url: "menu/index", //自己的接口地址
        method: "post", //请求方法
        data
    });
}
//调用菜单接口
function getMenuList() {
    getMenu({}).then(res => {
        if (res.err === 0) {
            //匹配路由addRoutes加入路由 router.addRoute
            routesArr.forEach((item, index) => { //自己的路由
                generateRoutes(res.data, item)
            })
            newRoutes.forEach(item => {
                router.addRoute('home', item); //添加
            })

        }
    })
}
//递归循环接口返回的路由
function generateRoutes(value, item) {
    value.forEach(e => {
        if (e.children) {
            generateRoutes(e.children, item)
        } else {
            if (item.path === '/' + e.route) {
                newRoutes.push(item)
            }
        }
    })
}
let flag = true

router.beforeEach((to, from, next) => {
    let token = sessionStorage.getItem('token') //获取 存储token
    if (to.path !== '/login') { //如果去其他页面
        if (token && flag) { //如果有sessionSt0orage
            getMenuList() //登录就获取菜单
        }
        if (token) {
            next(); //跳转
            flag = false
        } else {
            next('/login') //还在登录页
            flag = true
        }
    } else {
        next();
        flag = true
        newRoutes = []
    }
})
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch((err) => err);
};

export default router